package com.polaris.live.common.cacha

import com.polaris.live.common.constant.CacheConst
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.event.MatchingAssetEvent
import com.polaris.live.common.resp.AppVersionVo
import com.polaris.live.common.resp.UserResp
import com.tencent.mmkv.MMKV
import org.greenrobot.eventbus.EventBus

/**
 * 存用户相关信息
 *
 * @author Created by 半仙 on 2023/8/4/004 11:24
 */
object UserManager {

    @Volatile
    private var userInfo: UserResp? = null

    /**
     * 存用户信息
     */
    fun setUser(userResp: UserResp) {
        /**
         * 可以用来匹配的资产变化
         */
        if (userInfo?.call?.callAsset != userResp.call?.callAsset) {
            EventBus.getDefault().post(MatchingAssetEvent(userResp.call?.callAsset ?: false))
        }
        userInfo = userResp

        //展示页面
        handleDisplayUtil(userResp)

        val kv = MMKV.mmkvWithID(CacheConst.APP)
        kv.encode(CacheConst.user, userResp)
    }

    private fun handleDisplayUtil(userResp: UserResp) {
        userResp.extension?.landingPage?.let {
            DisplayUtil.setAppLandingPage(it)
        }
    }

    /**
     * 获取用户信息
     */
    fun getUser(): UserResp {
        return userInfo ?: let {
            val kv = MMKV.mmkvWithID(CacheConst.APP)
            val user = kv.decodeParcelable(CacheConst.user, UserResp::class.java)

            userInfo = user

            user ?: let {
                UserResp().apply { setUser(this) }
            }
        }
    }

    /**
     * 获取用户id
     */
    fun getUserId(): Long {
        return getUser().userId ?: 0L
    }

    /**
     * 获取是不是主播
     */
    fun getAgency(): Boolean {
        return getUser().profile?.agencyId != null || getUser().profile?.ownerAgencyId != null
    }
    /**
     * 是不是安全用户
     */
    fun isSafeUser(): Boolean {
        val safeLevel = getUser().flag?.safeLevel
        val safeType = getUser().flag?.safeType
        if (safeLevel == null || safeType == null) return false
        return safeLevel > 0 && safeType == CommonConst.SafeLevel.NORMAL
    }

    /**
     * 是否登录
     */
    fun isLogin(): Boolean {
        return getUser().userId != null
    }

    /**
     * 登出触发器
     */
    var onLogoutTrigger: OnLogoutTrigger? = null

    /**
     * 应用更新触发器
     */
    var onAppForceUpgrade: OnAppForceUpgrade? = null

    /**
     * 获取用户信息
     */
    const val ALL_INFORMATION = 0L                   //全部信息
    const val BASIC_INFORMATION = 1L shl 1           //基础信息
    const val LIVE_INFORMATION = 1L shl 5            //直播信息
    const val CALL_INFORMATION = 1L shl 6            //通话信息
    const val COVER_INFORMATION = 1L shl 7           //封面信息
    const val ALBUM_INFORMATION = 1L shl 8           //相册信息
    const val GIFT_INFORMATION = 1L shl 9            //礼物信息
    const val LABEL_INFORMATION = 1L shl 10          //标签信息
    const val REMARK_INFORMATION = 1L shl 11         //备注信息
    const val RELATIONSHIP_INFORMATION = 1L shl 12   //关系信息
    const val DECORATIVE_DISPLAY = 1L shl 13         //装饰展示
    const val HONOR_INFORMATION = 1L shl 14          //勋章信息
    const val GAME_CREDITS = 1L shl 15               //游戏积分
    const val BEAUTIFUL_NUMBER = 1L shl 16           //靓号

    //用户是否有充值特惠活动
    private var userRechargeAcState = true

    fun changeRechargeAcState(isHave: Boolean) {
        userRechargeAcState = isHave
    }

    fun getRechargeAcState() = userRechargeAcState

}

fun interface OnLogoutTrigger {

    fun onLogout(userId: Long, delay: Long)

}

fun interface OnAppForceUpgrade {

    fun onAppForceUpgrade(appVersionVo: AppVersionVo)

}